//
//  MTRecordsViewController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/22.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTRecordsViewController.h"
#import "MTWalletRecodedsCell.h"
@interface MTRecordsViewController ()


@property(nonatomic,assign) int page;
@property(nonatomic,strong) NSMutableArray *records;

@end

@implementation MTRecordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"交易记录";
    self.tableView.rowHeight = 64;
    self.tableView.contentInset = UIEdgeInsetsMake(3, 0, 0, 0);
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"MTWalletRecodedsCell" bundle:nil] forCellReuseIdentifier:@"MTWalletRecodedsCellID"];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 65, 0, 17);
    self.page = 1;
    @weakify(self);
    // 下拉刷刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.page = 1;
        [self getDataIsHeaderRefresh:YES];
    }];
    
    // 上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        self.page += 1;
        [self getDataIsHeaderRefresh:NO];
    }];
    [self getDataIsHeaderRefresh:YES];
    
}


#pragma mark - 请求所有数据方法
- (void)getDataIsHeaderRefresh:(BOOL)headerRefresh {
    [[MTWalletApiManager manager] getRecordsWithSymbol:_model.symbol
                                             page:self.page
                                            complation:^(MTCoinRecordsTotalModel *totalworks, MTError *error) {
        if(error == nil)
        {
            if (headerRefresh) {
                self.records = totalworks.list;
                [self.tableView reloadData];
            } else {
                if(totalworks.list.count == 0)
                {
                    self.page = self.page - 1;
                }
                [self.records addObjectsFromArray:totalworks.list];
                [self.tableView reloadData];
            }
            if (totalworks.last) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
        }
        if(headerRefresh)
        {
            [self.tableView.mj_header endRefreshing];
        }else
        {
            [self.tableView.mj_footer endRefreshing];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.records.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTWalletRecodedsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MTWalletRecodedsCellID"];
    cell.model = self.records[indexPath.row];
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
